﻿#-*- coding: utf-8 -*-
"""
Vérificateur d'anagrammes - par Guillaume Huysmans, 2013.
Utilisation : anagramme.py mot1 mot2

Nécessite le module unidecode (https://pypi.python.org/pypi/Unidecode)
Pour l'installer : décompresser l'archive, taper ces deux commandes
$ python setup.py install
$ python setup.py test
Sous Windows, assurez-vous d'ouvrir une console "en tant qu'admin".

Cet outil ne convient que pour des langues utilisant l'alphabet 
latin (pas le grec, par exemple). Pour les langues "exotiques", 
il faudrait utiliser un système de translittération...
"""

from unidecode import unidecode
import sys

def normalize(s, in_enc):
	return unidecode(unicode(s, in_enc))

def getLetterRank(c):
	"""
	Returns the rank of the letter c between 0 and 25.
	If c isn't a letter, -1 is returned.
	"""
	if (c>='A') and (c<='Z'):
		return ord(c)-65 #ord('A')
	elif (c>='a') and (c<='z'):
		return ord(c)-97 #ord('a')
	else:
		return -1

def areAnagrams(w1, w2, enc):
	#Normalize both inputs...
	w1 = normalize(w1, enc)
	w2 = normalize(w2, enc)
	#As they have different lengths, they can't be anagrams...
	if len(w1) != len(w2):
		return False
	#Array holding counters
	letters = [0] * 26
	#Loop...
	for i in range(0, len(w1)):
		#First word
		l = getLetterRank(w1[i])
		if l > -1:
			letters[l] += 1
		#Second word
		l = getLetterRank(w2[i])
		if l > -1:
			letters[l] -= 1
	#All counters should be zero
	for ct in letters:
		if ct:
			return False
	return True


if len(sys.argv) != 3:
	#print "about" informations and usage
	print unicode(__doc__.strip(), "utf-8") #utf-8 is the source code encoding
else:
	if sys.platform == 'win32':
		#French characters won't work if we don't force the input encoding
		enc = 'iso-8859-1'
	else:
		enc = sys.stdin.encoding
	print areAnagrams(sys.argv[1], sys.argv[2], enc)